<template>
  <div :class="className" :style="{ height: height, width: width }">
    <!-- 年份选择器 -->
    <div class="year-selector" :style="{ position: 'absolute', top: '35px', left: '20px' }">
      <el-select v-model="selectedYear" placeholder="选择年份" @change="handleYearChange">
        <el-option
          v-for="year in availableYears"
          :key="year"
          :label="year"
          :value="year">
        </el-option>
      </el-select>
    </div>
    <!-- 图表容器 -->
    <div ref="chartContainer" style="width: 100%; height: 100%;" />
  </div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      selectedYear: new Date().getFullYear(), // 默认选择当前年份
      availableYears: [2021, 2022, 2023, 2024] // 可用的年份数组
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.fetchChartData(this.selectedYear) // 初始加载数据
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chartContainer, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLabel: {
            rotate: 60 // 旋转60度
          }
        },
        grid: {
          left: 10,
          right: 20,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLabel: {
            show: true,
            formatter: function(value) { return value.toFixed(1) + 'mg/L' }
          }
        },
        legend: {
          data: ['化学需氧量']
        },
        series: [{
          name: '化学需氧量',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: expectedData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }]
      })
    },
    fetchChartData(year) {
      // 根据年份获取数据的逻辑
      // 这里使用静态数据作为示例，实际应用中应替换为 API 请求
      const data = {
        '2021': { expectedData: [120, 200, 150, 130, 110, 100, 90, 80, 70, 60, 50, 40] },
        '2022': { expectedData: [130, 210, 160, 140, 120, 110, 100, 90, 80, 70, 60, 50] },
        '2023': { expectedData: [140, 220, 170, 150, 130, 120, 110, 100, 90, 80, 70, 60] },
        '2024': { expectedData: [8.0, 8.0, 7.0, 5.0, 3.0, 4.0, 5.0, 5.0, 4.0, 5.0, 6.0, 7.0] }
      }[year]
      this.setOptions(data)
    },
    handleYearChange(year) {
      this.selectedYear = year
      this.fetchChartData(year)
    }
  }
}
</script>

<style scoped>
.year-selector /deep/ .el-select {
  width: 100px; /* 调整选择器的宽度 */
  height: 30px; /* 调整选择器的高度 */
}

.year-selector /deep/ .el-input__inner {
  height: 30px; /* 调整输入框的高度 */
  line-height: 30px; /* 调整行高以垂直居中文本 */
}

.year-selector {
  z-index: 2; /* 确保选择器在图表上方 */
}
</style>

